package com.ants.platform.core.plugins.ehcache;

import com.alibaba.fastjson.JSON;
import com.ants.platform.core.utils.StringEncryptUtil;
import org.springframework.cache.interceptor.KeyGenerator;

import java.lang.reflect.Method;

/**
 * 自定义key生成策略解决不能缓存实体参数
 * Created by Administrator on 2016/7/31.
 */
public class CacheKeyGenerator implements KeyGenerator {


    @Override
    public Object generate(Object o, Method method, Object... objects) {
        return ecodeStr(o, method, objects);
    }

    //自定义生成EhCache Key
    private String ecodeStr(Object o, Method method, Object... objects) {
        StringBuffer sb = new StringBuffer();
        for (Object obj : objects) {
            if (obj != null) {
                sb.append(JSON.toJSONString(obj));
            }
        }
        return StringEncryptUtil.MD5(sb.toString());
    }
}
